const { BaseCommand } = require("./base");
const { moveCursorLineUp } = require("../utils/interaction_tool");
const { info, log } = require("../utils/log_tool");

class InputCommand extends BaseCommand {
    async execute() {
        const readline = require("node:readline");
        const data = this.selfData;
        const prompt = data && (data.prompt || this.selfCfg.title);
        const default_value = data?.default;
        if (prompt) {
            info(prompt);
        }
        const input = await new Promise((resolve) => {
            const rl = readline.createInterface({
                input: process.stdin,
                output: null,
            });
            rl.question("", (input) => {
                info("");
                resolve(input);
                rl.close();
            });

            rl.on("close", () => {});
        });
        // 往上移一行
        moveCursorLineUp(1);
        return input || default_value || "";
    }
}

module.exports = {
    InputCommand,
};
